﻿/*
 * File: AdventureEditWindow.cs
 * Developer : Brent Larsen (brentoboy@gmail.com)
 * Date: 2011-AUG-9
 * Copyright (c) 2011 Brent Larsen.
 * Distributed under the terms of the GPL v3
 *  
 * This file is part of GenX-RPG
 * http://code.google.com/p/genx-rpg/ 
 * 
 * GenX-RPG is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GenX-RPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GenX-RPG.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rpg.Editor
{
    public partial class AdventureEditWindow : Form
    {
        private AdventureDatabase _db;
        private Adventure _adventure;

        public AdventureEditWindow()
        {
            InitializeComponent();        
        }

        public AdventureEditWindow(AdventureDatabase db)
            : this()
        {
            Contract.Requires(db != null);
            _db = db;
            _adventure = _db.GetAdventureInfo();

            list_StartMap.DisplayMember = "Value";
            list_StartMap.ValueMember = "Key";
            list_StartMap.DataSource = _db.GetMapNames().ToKvpList();

            this.Text = "Adventure Properties";
            txtName.Text = _adventure.Name;
            //txtCopyrightOwner.Text = _adventure.CopyrightOwner;
            //txtDateOriginallyPublished.Text = _adventure.DateOriginallyPublished;
            //txtCopyrightNotice.Text = _adventure.CopyrightNotice;
            //txtLicense.Text = _adventure.License;            
            list_StartMap.SelectedValue = _adventure.StartLocation.MapId;
            txtStartX.Text = _adventure.StartLocation.X.ToString();
            txtStartY.Text = _adventure.StartLocation.Y.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _adventure.Name = txtName.Text;
            //_adventure.CopyrightOwner = txtCopyrightOwner.Text;
            //_adventure.DateOriginallyPublished = txtDateOriginallyPublished.Text;
            //_adventure.CopyrightNotice = txtCopyrightNotice.Text;
            //_adventure.License = txtLicense.Text;
            _adventure.StartLocation.MapId = list_StartMap.SelectedValue.AsInt() ?? 0;
            _adventure.StartLocation.X = txtStartX.Text.AsInt() ?? 0;
            _adventure.StartLocation.Y = txtStartY.Text.AsInt() ?? 0;

            _db.UpdateAdventure(_adventure); 

            var menuItem = Program.MainMenuWindow.menuFile.DropDownItems.OfType<ToolStripMenuItem>().Single(x => x.Tag.ToString_Safe() == _db.AdventureFolder);
            menuItem.Text = "{0} ({1})".f(txtName.Text, _db.AdventureFolder);

            this.Close();
        }
    }
}
